<?php namespace App\Controllers\Admin;

// =====================================================================
// DASHBOARD METHODS
// =====================================================================

class DashboardController extends AdminController {
    
    // ---------------------------------------------------------------------        
    // getDashboard, display a dashboard with general information.
    // ---------------------------------------------------------------------            
    public function Index() {

        $site_id = \Analytics::getSiteIdByUrl('http://tallermecanicodario.com.ar'); // return something like 'ga:11111111'
        $stats = \Analytics::query($site_id, '30daysAgo', 'today', 'ga:visits,ga:pageviews,ga:avgSessionDuration');
        $data['visits'] = $stats->totalsForAllResults['ga:visits'];
        $data['pageviews'] = $stats->totalsForAllResults['ga:pageviews'];
        $data['timeonsite'] = $stats->totalsForAllResults['ga:avgSessionDuration']/60;
        
        $sources = \Analytics::query($site_id, '30daysAgo', 'today', 'ga:visits,ga:pageviews', array('dimensions'=>'ga:Source'));        
        foreach($sources->rows as $key => $value)
        {
            $data['sources'][$value[0]]['visits'] = $value[1];
            $data['sources'][$value[0]]['pageviews'] = $value[2];
        }

        $keywords = \Analytics::query($site_id, '30daysAgo', 'today', 'ga:searchUniques', array('dimensions'=>'ga:Keyword'));
        foreach($keywords->rows as $key => $value) {
            if($value[0] == "(not provided)" OR $value[0] == "(not set)") continue;
            $data['keywords'][] = $value[0];
        }
                
        $this->layout->content = \View::make('admin.dashboard')->with('data', $data);
    }
}
    
